package com.mkz.jbpm.controller;

import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.mkz.core.common.util.ResponseResult;
import com.mkz.core.log.annotation.SysLog;
import com.mkz.core.security.util.SecurityUtils;
import com.mkz.jbpm.entity.FinishedTask;
import com.mkz.jbpm.service.FinishedTaskService;

import org.springframework.security.access.prepost.PreAuthorize;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.AllArgsConstructor;
import org.springframework.web.bind.annotation.*;


/**
 * 已办任务
 *
 * @author KuangJianyu
 * @date 2020-04-08 15:00:18
 */
@RestController
@AllArgsConstructor
@RequestMapping("/finishedtask" )
@Api(value = "finishedtask", tags = "已办任务管理")
public class FinishedTaskController {

    private final  FinishedTaskService finishedTaskService;

    /**
     * 分页查询
     * @param page 分页对象
     * @param finishedTask 已办任务
     * @return
     */
    @ApiOperation(value = "分页查询", notes = "分页查询")
    @GetMapping("/page" )
    public ResponseResult getFinishedTaskPage(Page page, FinishedTask finishedTask) {
    	if(null==finishedTask) {
    		finishedTask = new FinishedTask();
    	}
    	finishedTask.setAssignee(SecurityUtils.getUser().getUsername());
        return ResponseResult.ok(finishedTaskService.page(page, Wrappers.query(finishedTask)));
    }


    /**
     * 通过id查询已办任务
     * @param id id
     * @return ResponseResult
     */
    @ApiOperation(value = "通过id查询", notes = "通过id查询")
    @GetMapping("/{id}" )
    public ResponseResult getById(@PathVariable("id" ) String id) {
        return ResponseResult.ok(finishedTaskService.getById(id));
    }

    /**
     * 新增已办任务
     * @param finishedTask 已办任务
     * @return ResponseResult
     */
    @ApiOperation(value = "新增已办任务", notes = "新增已办任务")
    @SysLog("新增已办任务" )
    @PostMapping
    @PreAuthorize("@pms.hasPermission('jbpm_finishedtask_add')" )
    public ResponseResult save(@RequestBody FinishedTask finishedTask) {
        return ResponseResult.ok(finishedTaskService.save(finishedTask));
    }

    /**
     * 修改已办任务
     * @param finishedTask 已办任务
     * @return ResponseResult
     */
    @ApiOperation(value = "修改已办任务", notes = "修改已办任务")
    @SysLog("修改已办任务" )
    @PutMapping
    @PreAuthorize("@pms.hasPermission('jbpm_finishedtask_edit')" )
    public ResponseResult updateById(@RequestBody FinishedTask finishedTask) {
        return ResponseResult.ok(finishedTaskService.updateById(finishedTask));
    }

    /**
     * 通过id删除已办任务
     * @param id id
     * @return ResponseResult
     */
    @ApiOperation(value = "通过id删除已办任务", notes = "通过id删除已办任务")
    @SysLog("通过id删除已办任务" )
    @DeleteMapping("/{id}" )
    @PreAuthorize("@pms.hasPermission('jbpm_finishedtask_del')" )
    public ResponseResult removeById(@PathVariable String id) {
        return ResponseResult.ok(finishedTaskService.removeById(id));
    }

}
